package com.xbongbong.sso.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.sso.pojo.dto.OauthUserAddDTO;
import com.xbongbong.sso.pojo.dto.OauthUserDetailDTO;
import com.xbongbong.sso.pojo.dto.OauthUserUpdateDTO;
import com.xbongbong.sso.pojo.vo.OauthUserAddVO;
import com.xbongbong.sso.pojo.vo.OauthUserDetailVO;
import com.xbongbong.sso.pojo.vo.OauthUserUpdateVO;
import com.xbongbong.sso.service.OauthUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * IDP用户对应唯一码
 * 创建时间 2020/12/28 2:51 PM
 * 修改时间 2020/12/28 2:51 PM
 * @author chy
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/oauthUser")
public class OauthUserController {

    private static final Logger LOG = LoggerFactory.getLogger(OauthUserController.class);

    @Resource
    private OauthUserService oauthUserService;


    /**
     * IDP用户对应唯一码详情
     * @param oauthUserDetailDTO 关联DTO
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String detail(@RequestBody @Valid OauthUserDetailDTO oauthUserDetailDTO, BindingResult br) throws Exception{
        XbbResponse<OauthUserDetailVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthUserDetailVO oauthUserDetailVO = oauthUserService.detail(oauthUserDetailDTO);
                response = new XbbResponse<>(oauthUserDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * IDP用户对应唯一码新建
     * @param oauthUserAddDTO 关联DTO
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid OauthUserAddDTO oauthUserAddDTO, BindingResult br) throws Exception{
        XbbResponse<OauthUserAddVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthUserAddVO oauthUserAddVO = oauthUserService.add(oauthUserAddDTO);
                response = new XbbResponse<>(oauthUserAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * IDP用户对应唯一码编辑
     * @param oauthUserUpdateDTO 关联DTO
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid OauthUserUpdateDTO oauthUserUpdateDTO, BindingResult br) throws Exception{
        XbbResponse<OauthUserUpdateVO> response = null;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                OauthUserUpdateVO oauthUserUpdateVO = oauthUserService.update(oauthUserUpdateDTO);
                response = new XbbResponse<>(oauthUserUpdateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
